<template>
  <view class="content">
    <div class="btnBox">
      <button @click="startBtn" type="default" style="background-color: #FFA91B;border-radius: 50px;">
        <span style="color: white;font-family: '微软雅黑';">开始制作</span>
      </button>
    </div>
  </view>
</template>

<script>
  export default {
    components: {},
    data() {
      return {
        hasWeixinAuth: false
      };
    },

    onLoad() {
      const vm = this;
      vm.loginByWeixin();
    },

    methods: {
      // 点击启动跳往扫描身份证页面
      startBtn() {
        uni.navigateTo({
          url: './productionWorks'
        });
      },

      getWeixinCode() {
        return new Promise((resolve, reject) => {
          uni.login({
            provider: 'weixin',
            success(res) {
              resolve(res.code)
            },
            fail(err) {
              reject(new Error('微信登录失败'))
            }
          })
        })
      },

      loginByWeixin() {
        this.getWeixinCode().then((code) => {
          console.log(code);
          return uniCloud.callFunction({
            name: 'login-by-weixin',
            data: {
              code
            }
          })
        }).then((res) => {
          console.log(res)
          /* uni.showModal({
            showCancel: false,
            content: JSON.stringify(res.result)
          }); */
          if (res.result.code === 0) {
            uni.setStorageSync('uniIdToken', res.result.token);
            uni.setStorageSync('openid', res.result.openid);
          }
        }).catch((err) => {
          uni.showModal({
            showCancel: false,
            content: '微信登录失败，请稍后再试'
          })
        })
      }
    }
  };
</script>

<style lang="scss" scoped>
  .content {
    height: 100vh;
    background: #efefef;
    display: flex;
    justify-content: center;
    align-items: center;

    .btnBox {
      width: 80%;
    }
  }
</style>
